package com.taotao.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sun.tools.corba.se.idl.constExpr.BooleanNot;
import com.taotao.pojo.User;
import com.taotao.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * Created by liujunchen on 2016/11/23.
 */

@RequestMapping("new/user")
@Controller
//查询接口
public class NewUserController  {
    //根据用户ID查询用户信息
    @Autowired
    private UserService  userService  ;
    @RequestMapping(value = "{id}"  , method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<User> queryById(@PathVariable("id") Long  id )    {
        try {
            User user =  this.userService.queryUserById(id);
            if (null == user) {
                //资源不存在，响应不存在
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            //资源存在， 响应200
//        return    ResponseEntity.status(HttpStatus.OK).body(user) ;
            return ResponseEntity.ok(user);
        } catch ( Exception  a  )
        {
                a.printStackTrace() ;
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        @RequestMapping(method = RequestMethod.POST)
        public ResponseEntity<Void>saveUser(User user ){
            try {
                Boolean bool = this.userService.saveUser(user);
                if (bool) {//新增成功，响应201
                    return ResponseEntity.status(HttpStatus.CREATED).build();
                }
            }catch(Exception e  ){
                e.printStackTrace();
                }
            //新增失败响应500
            return   ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() ;
        }


        @RequestMapping(method = RequestMethod.PUT)
            public   ResponseEntity<Void>updateUser(User user )  {
            try {
                Boolean bool = this.userService.updateUser(user);
                if (bool) {//更新成功，响应201
                    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
                }
            }catch(Exception e  ){
                e.printStackTrace();
            }
            //新增失败响应500
            return   ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() ;

        }
    @RequestMapping(method = RequestMethod.DELETE)
    public   ResponseEntity<Void>deleteUser(@RequestParam(value = "id",defaultValue = "0") Long id ){
        try {
            if(id.longValue()==0)
                //没有参数， 响应状态码400
            return  ResponseEntity.status(HttpStatus.BAD_REQUEST).build() ;
            Boolean bool  = this.userService.deleteUser(id)  ;
            if(bool){
                //更新成功 响应204
                return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        }catch(Exception e  ){
            e.printStackTrace();
        }
        //新增失败响应500
        return   ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() ;

    }



}
